/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cem.cemweb.person_module.testing;

import org.cem.cemweb.util.HibernateFactory;
import org.cem.cemweb.person_module.objects.*;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
/**
 *
 * This is going to create the default entries for the database. This should
 * be called after the schema is created.
 */
public class InsertTest {
    private static Log log= LogFactory.getLog(InsertTest.class);
    public static void main(String[] args){
        Session session=null;
        Transaction tx = null;
        
        try{
            HibernateFactory.buildSessionFactory();
            session = HibernateFactory.openSession();
            tx = session.beginTransaction();
            log.info("Starting.");
            Departments dept=getDepartments();
            
            States state_ = getStates();
            //session.save(state_);
            
            Zipcodes zipcode=getZipcodes();
            zipcode.setState_id(state_);
            //session.save(zipcode);
            Addresses address = getAddresses();
            address.setZipcode_id(zipcode);
            //session.save(address);
            log.info("Should be done");
            
            Locations location=getLocations();
            location.setAddress_id(address);
            location.setDept_id(dept);
            
            Roles role=getRoles();
            role.setDept_id(dept);
            
            Languages language=getLanguages();
            Educational_Level education_level=getEducational_level();
            
            Contacts contact = getContacts();
            
            Ethnicity ethnicity = getEthnicity();
            
            Users user = getUsers();
            
            People person = getPeople();
            person.setAddress_id(address);
            person.setContact_id(contact);
            person.setEthnicity_primary_id(ethnicity);
            person.setEthnicity_secondary_id(ethnicity);
            person.setLanguage_id(language);
            person.setEducational_level_id(education_level);
            person.setUser_id(user);
            person.setRole_id(role);
          
            Images image = getImages();
            image.setPerson_id(person);
            
            Vehicles vehicle=getVehicles();
            vehicle.setPerson_id(person);
             
            
            session.save(dept);
            session.save(state_);
            session.save(zipcode);
            session.save(address);
            
            session.save(location);
           
            session.save(role);
            
            session.save(language);
            session.save(education_level);
             
            session.save(contact);
             
            session.save(ethnicity);
            
            session.save(user);
           
            session.save(person);
           
            session.save(image);
            session.save(vehicle);
           
            
            session.flush();
            tx.commit();
            log.info("InsertTest:main: Default values inserted");
        }catch(HibernateException e){
            HibernateFactory.rollback(tx);
            log.error("InsertTest:main: Exception occurred while inserting defaults",e);
            throw e;
        }finally{
            HibernateFactory.close(session);
            HibernateFactory.closeFactory();
        }
    }
    
    private static Addresses getAddresses(){
        Addresses address= new Addresses();
        address.setStreet_address("Default street");
        address.setSec_street_address("Default second street");
        
        return address;
    }
    
    private static Contacts getContacts(){
        Contacts contact = new Contacts();
        contact.setDt_phone("(000)000-0000");
        contact.setEmail("Default email");
        contact.setEmployer("Default Employer");
        contact.setEt_phone("(000)000-0000");
        contact.setMobile_phone("(000)000-0000");
        contact.setPager("(000)000-0000");
        contact.setWk_phone("(000)000-0000");
        
        return contact;
    }
    
    private static Departments getDepartments(){
        Departments dept= new Departments();
        dept.setCode("Default code");
        dept.setName("Default department");
        dept.setDescr("Default department description");
        return dept;
    }
    
    private static Educational_Level getEducational_level(){
        Educational_Level edu= new Educational_Level();
        edu.setCode("Default code");
        edu.setName("Default educational level");
        edu.setValue("Default value");
        return edu;
    }
    
    private static Emergency_Info getEmergency_Info(){
        Emergency_Info emergency_info= new Emergency_Info();
        emergency_info.setEm_type("Default");
        
        return emergency_info;
    }
    
    private static Ethnicity getEthnicity(){
        Ethnicity ethnicity = new Ethnicity();
            ethnicity.setCode("Code");
            ethnicity.setDescr("Default descr");
            ethnicity.setDis_id(1);
            ethnicity.setHas_subset(new Integer(0));
            ethnicity.setIs_subset_of(new Long(1));
        return ethnicity;
    }
    
    private static Images getImages(){
        Images image = new Images();
        image.setDate_entered(new java.util.Date());
        image.setFp_image_path("Path to figureprint");
        image.setIs_current(1);
        image.setLg_image_path("Default large image");
        image.setSm_image_path("Default small image");
        
        return image;
    }
    
    private static Languages getLanguages(){
        Languages language= new Languages();
        language.setName("Default Language");
        
        return language;
    }
    
    private static Locations getLocations(){
        Locations location= new Locations();
        location.setBuilding("Default building");
        location.setRoom("Default Room");
        
        return location;
    }
    
    private static People getPeople(){
        People person= new People();
        person.setFname("Default fname");
        person.setLname("Default lname");
        person.setMname("Default mname");
        person.setSex("M");
        person.setDate_of_birth(new java.util.Date());
        person.setBirth_location("Default location");
        return person;
    }
    
    private static Roles getRoles(){
        Roles role= new Roles();
        role.setName("Default role");
        role.setRole_group("Default group");
        return role;
    }
    
    private static States getStates(){
        States state_ = new States();
        state_.setCountry("United States of America");
        //state_.setId(new Long(1));
        state_.setLname("California");
        state_.setMname("Calif");
        state_.setSname("CA");
        return state_;
    }
    
    private static Users getUsers(){
        Users user= new Users();
        user.setName("Default");
        user.setBlocked(1);
        user.setPasswd("Default");
        user.setAtt_logins(100);
        return user;
    }
    
    private static Vehicles getVehicles(){
        Vehicles vehicle = new Vehicles();
        vehicle.setColor("Default color");
        vehicle.setDate_entered(new java.util.Date());
        vehicle.setIs_current(1);
        vehicle.setLicense_plate("Default");
        vehicle.setMake("Default make");
        vehicle.setModel("Default model");
        vehicle.setPass_id("Default pass");
        vehicle.setYear("2004");
        
        return vehicle;
    }
    
    private static Zipcodes getZipcodes(){
        Zipcodes zip = new Zipcodes();
        zip.setCity("Default City");
        zip.setCounty("Default County");
        zip.setZipcode("00000-0000");
        return zip;
    }
    
    private static Relationships getRelationships(){
        Relationships relation = new Relationships();
        relation.setCode("Default code");
        relation.setName("Default relation");
        relation.setWeight(0);
        
        return relation;
    }
    

}
